/*
 * Copyright 2022-2029 heshuixing(https://gitee.com/heshuixing)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mercury.ec.module.air.controller.admin.order.validator;

import com.mercury.ec.framework.common.util.validation.ValidationUtils;
import com.mercury.ec.module.air.controller.admin.order.vo.order.AirOrderIssuedReqVO;
import com.mercury.ec.module.air.enums.AirOrderReturnTypeEnum;
import com.mercury.ec.module.air.enums.AirOrderTypeEnum;
import com.mercury.ec.module.air.enums.CustomerAgreementTypeEnum;
import com.mercury.ec.module.air.validator.AirGroup;
import com.mercury.ec.module.basic.enums.BasicAirTicketTypeEnum;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 订单已出票校验器
 *
 * @author hesx
 */
public class AirOrderIssuedSequenceProvider implements DefaultGroupSequenceProvider<AirOrderIssuedReqVO> {
    @Override
    public List<Class<?>> getValidationGroups(AirOrderIssuedReqVO issuedReqVO) {
        List<Class<?>> defaultGroupSequence = new ArrayList<>();
        defaultGroupSequence.add(AirOrderIssuedReqVO.class);
        if (Objects.nonNull(issuedReqVO)) {
            if (AirOrderTypeEnum.SALE.getType().equals(issuedReqVO.getType())) {
                defaultGroupSequence.add(AirGroup.Sale.class);
            } else if (AirOrderTypeEnum.CHANGE.getType().equals(issuedReqVO.getType())) {
                defaultGroupSequence.add(AirGroup.Change.class);
            } else if (AirOrderTypeEnum.RETURN.getType().equals(issuedReqVO.getType())) {
                defaultGroupSequence.add(AirGroup.Return.class);
                if (AirOrderReturnTypeEnum.WILL_RETURN.getType().equals(issuedReqVO.getFinallyReturnType())) {
                    defaultGroupSequence.add(AirGroup.WillReturn.class);
                } else if (AirOrderReturnTypeEnum.UNWILL_RETURN.getType().equals(issuedReqVO.getFinallyReturnType())) {
                    defaultGroupSequence.add(AirGroup.UnWillReturn.class);
                }
            } else if (AirOrderTypeEnum.ADJUST.getType().equals(issuedReqVO.getType())) {
                defaultGroupSequence.add(AirGroup.Adjust.class);
            }
            if (CustomerAgreementTypeEnum.isCustomerAgreement(issuedReqVO.getCustomerAgreementType())) {
                defaultGroupSequence.add(AirGroup.CustomerAgreement.class);
            }
            if ((AirOrderTypeEnum.isSale(issuedReqVO.getType()) || AirOrderTypeEnum.isChange(issuedReqVO.getType()))
                    && (!BasicAirTicketTypeEnum.isB2c(issuedReqVO.getTicketType()) && !BasicAirTicketTypeEnum.isOutsourcing(issuedReqVO.getTicketType()))) {
                defaultGroupSequence.add(AirGroup.IssueOfficeRequired.class);
            }
            if (CustomerAgreementTypeEnum.isGp(issuedReqVO.getCustomerAgreementType())) {
                issuedReqVO.getPassengers().forEach(p -> ValidationUtils.validate(p, AirGroup.Gp.class));
            }
        }
        return defaultGroupSequence;
    }
}
